/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmall.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.eyaomall.wxmall.common.helper.EmployeeHelper;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.common.helper.GlobEnv;
/**
 * 店员拦截器
 * @author pro
 *
 */
public class EmployeeInterceptor extends HandlerInterceptorAdapter {
	
	private static final Logger LOG = LoggerFactory.getLogger(EmployeeInterceptor.class);

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String shopId = request.getParameter("shopId");
		if(StringUtil.isNotEmpty(shopId)){
			
			String sId = (String) request.getSession().getAttribute("employee_shop_id");
			
			LOG.error("shopId:" + shopId + "sId:" + sId);
			if(StringUtil.isNotEmpty(sId) && !shopId.equalsIgnoreCase(sId)){
				EmployeeHelper.cleanAll();
				response.sendRedirect(GlobEnv.getWebURL("employee/login") + "?shopId=" + shopId);
				return false;
			}
			
			request.getSession().setAttribute("employee_shop_id", shopId);
		}
		
		if(EmployeeHelper.getId() == null || EmployeeHelper.getId() <= 0){
			response.sendRedirect(GlobEnv.getWebURL("employee/login"));
			return false;
		}
		
		return true;
	}
}
